#!/bin/sh
# PCP QA Test No. 1145
# Use libpcp_fault to exercise error paths in vpmprintf()
#
# Copyright (c) 2019 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

src/check_fault_injection >/dev/null 2>&1 || \
    _notrun "libpcp not built with fault injection enabled"

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

export PM_FAULT_CONTROL=$tmp.control
export LD_PRELOAD=$PCP_LIB_DIR/libpcp_fault.so

# [Tue Feb  5 07:03:01] pcp(6045) Warning: vmprintf malloc: malloc(4096) failed: Cannot allocate memory
#
_filter()
{
    sed \
	-e 's/pcp([0-9][0-9]*)/pcp(PID)/' \
	-e 's/\[[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]]/[DATE]/' \
    # end
}

# real QA test starts here

echo "force 1st malloc to fail"
cat >$tmp.control <<End-of-File
libpcp/util.c:1	== 1
End-of-File
src/pmprintf "foo" "bar" 2>&1 | _filter

# k is 1024 bytes long
#
k="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123"

echo "force 1st realloc to fail"
cat >$tmp.control <<End-of-File
libpcp/util.c:2	== 1
End-of-File
src/pmprintf "X" $k "X" $k "X" $k "X" $k "X" 2>&1 \
| _filter

echo "force 2nd realloc to fail"
cat >$tmp.control <<End-of-File
libpcp/util.c:2	== 2
End-of-File
src/pmprintf "X" $k "X" $k "X" $k "X" $k "X" $k "X" $k "X" $k "X" $k "X" 2>&1 \
| _filter

# success, all done
status=0

exit

